<?php
/**
 * This file is part of the UXLabs project.
 *
 * (c) UXLabs project <http://code.uxlabs.me>
 *
 */

namespace uxl\app\api;

use Yii;
use yii\base\BootstrapInterface;

/**
 * module bootstrap class
 * @package uxl\app\api
 * @author ice deng <ice.deng@qq.com>
 */
class Bootstrap implements BootstrapInterface
{
    /**
     * @var array Model's map
     */
    private $_modelMap = [
        'AccessToken' => 'uxl\app\api\models\AccessToken',
        'AppToken' => 'uxl\app\api\models\AppToken',
        'AccessLog' => 'uxl\app\api\models\AccessLog',
    ];
    /**
     * @var string 访问密钥的有效期
     */
    public static $accessKeyExpiresIn = -1;
    /**
     * @var string 接口方法配置文件
     */
    public static $methodConfig = null;

    /**
     * @inheritdoc
     */
    public function bootstrap($app)
    {
        /** @var Module $module */
        $module = null;
        /** @var \yii\base\Module $m */
        foreach ($app->getModules() as $m) {
            if ($m instanceof Module) {
                $module = $m;
            }
        }
        // 更新数据model
        if($module) {
            $this->_modelMap = array_merge($this->_modelMap, $module->modelMap);
        }
        foreach ($this->_modelMap as $name => $definition) {
            $class = "uxl\\app\\api\\models\\" . $name;
            Yii::$container->set($class, $definition);
        }
        // 更新有效期
        if ($module && $module->accessKeyExpiresIn > -1) {
            static::$accessKeyExpiresIn = $module->accessKeyExpiresIn;
        }
        // 更新接口的路径配置
        if ($module && $module->methodConfig != null) {
            static::$methodConfig = $module->methodConfig;
        }
    }
}